home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / mhs_c.arc / INPOST.ARC / OPENFILE.C < prev    next >
C/C++ Source or Header  |  1988-06-27  |  2KB  |  86 lines

  1. /* ****************************** OPENFILE.C ****************************** */
  2. #include "cctypes.h"
  3.  
  4. extern int InFile;
  5. extern int OutFile;
  6. extern char InFileName[];
  7. extern char OutFileName[];
  8. extern char InExtension[];
  9.  
  10. int OpenInputFile()
  11. {
  12.     char *p;
  13.     int returnCode = NO_ERROR;
  14.     long fileSize = 0L;
  15.  
  16.     /* check for illegal extensions */
  17.     p = strrchr(InFileName,'.');
  18.     if ( p != NULL ) {
  19.         if ( strcmp(p,InExtension) != 0 ) {
  20. /*             Error(INVALID_FILENAME); */
  21.             returnCode = -1;
  22.             goto Out;
  23.         }
  24.     }
  25.     /* open the input file (read) */
  26.     InFile = open(InFileName, O_RDONLY | O_RAW, 0);
  27.     if ( (InFile == -1) &&
  28.         (strchr(&InFileName[strlen(InFileName) - 4], '.') == NULL ))
  29.     {
  30.         /* add the extension if not already there */
  31.         strcat(InFileName, InExtension);
  32.         InFile = open(InFileName, O_RDONLY | O_RAW, 0);
  33.     }
  34.     if ( InFile == -1 )
  35.         returnCode = -1;
  36.     else {
  37.         fileSize = lseek(InFile,(long)0,2);
  38.         if ( fileSize == 0L )
  39.             returnCode = -1;
  40.         fileSize = lseek(InFile,(long)0,0);
  41.     }
  42.  
  43. Out:
  44.     return(returnCode);
  45. }
  46.  
  47. int OpenOutputFile(extension)
  48. char *extension;
  49. {
  50.     int returnCode = NO_ERROR;
  51.     char *p;
  52.     FILE *tempHandle;
  53.  
  54.     /* make the output (MCB) file in the same directory */
  55.     strcpy(OutFileName,InFileName);
  56. Again:
  57.     p = strrchr(OutFileName,'.');
  58.     if ( p == NULL ) {
  59.         strcat(OutFileName,".");
  60.         goto Again;
  61.     }
  62.     strcpy(p,extension);
  63.  
  64.     tempHandle = fopen(OutFileName, "r");
  65.     if ( tempHandle != NULL ) {
  66.         /* already exists */
  67.         fclose(tempHandle);
  68.         unlink(OutFileName); /* it should do this already if using 0x8301 below */
  69.     }
  70.  
  71.     /* open/create the MCB file for writing */
  72.     OutFile = open(OutFileName, 0x8301, 0);
  73.     if ( OutFile == -1 ) {
  74.         Error(CANNOT_CREATE_OUTPUT);
  75.         goto CantDo;
  76.     }
  77.     goto Out;
  78. CantDo:
  79.     returnCode = CANNOT_CREATE_OUTPUT;
  80. Out:
  81.     return(returnCode);
  82. }
  83.  
  84.  
  85.  
  86.